﻿package com.cilico.single.components;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.cilico.single.ConfigManager;
import com.cilico.single.R;
import com.cilico.single.utils.ConstantRepository;
import com.cilico.single.utils.Utils;
import com.cilico.single.view.BaseView;
import com.cilico.single.view.DetailView;
import com.cilico.single.view.FormLayout;
import com.cilico.single.view.MainView;

/**
 * 九宫格icon
 * 
 * @author lv.xiangan
 *
 */
public class ImageItem extends ImageView implements Component {
	public CssStyle cssStyle_;
	// The component's left/top/width/height.
	int left_; // Relative left position to the left of the FULL canvas.
	int top_; // Relative left position to the top of the FULL canvas.
	public int width_;
	public int height_;
	boolean focusable_;
	public String attrValue_;
	private static final int BORDER_WIDTH = 5;
	int borderWidth_ = BORDER_WIDTH;
	Bitmap img_;
	Bitmap img_1 = null;
	Bitmap img_2 = null;
	public boolean isHyperLink_;
	boolean currentSelected = false;
	public boolean isMove_;
	String link_;
	public String linkAddr_;
	String descriptionText_;
	private TextPaint font_;
	private Bitmap focusImg_; // The image is which will be shown if this obj is
	private int marginTop_;
	private int marginRight_;
	private int marginLeft_;
	private int marginBottom_;
	private boolean isInTable_;
	private BaseView bv_;
	String isEncrypt_;
	int notice_;
	private int touchDownX_ = 0;
	private int touchDownY_ = 0;
	private int touchUpX_ = 0;
	private int touchUpY_ = 0;
	private ViewGroup.LayoutParams viewPara_ = null;
	
	public ImageItem(Context context) {
		super(context);
		bv_ = (BaseView) context;
	}

	public ImageItem(Context context, Bitmap img) {
		super(context);
		bv_ = (BaseView) context;
		// TODO Auto-generated constructor stub
		if (img == null) {
			throw new NullPointerException("The image is NULL!");
		}
		img_ = img;
		setImage(img_);
	}
	
	public ImageItem(Context context, Bitmap img, int valid) {
		super(context);
		notice_ = valid;
		bv_ = (BaseView) context;

		// TODO Auto-generated constructor stub

		if (img == null) {
			throw new NullPointerException("The image is NULL!");
		}
		img_ = img;
		setImage(img_);
	}

	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(width_, height_);
	}

	/*
	 * paint the ImageItem.
	 * 
	 */
	public void onDraw(Canvas g) {
		// TODO Auto-generated method stub
		if (img_ == null) {
			return;
		}
		
		int imgWidth = img_.getWidth();
		final int posY = marginTop_ + borderWidth_;
		if (focusable_ && focusImg_ != null){
			g.drawBitmap(focusImg_, (width_ - imgWidth) >> 1, posY, font_);
		} else{
			g.drawBitmap(img_, (width_ - imgWidth) >> 1 - 5, posY, font_);
		}
		if (descriptionText_ != null) {
			font_.setColor(0xff1d5175);
			if (bv_ instanceof MainView) {
				font_.setTextSize(Utils.getScaledValue(ConfigManager.SIZE_MAIN));
			}else {
				font_.setTextSize(Utils.getScaledValue(15));
			}
			// 设置字体非抗锯齿效果，使字体边界更平滑
			font_.setAntiAlias(true);
			BaseView.setTypeFace(font_, cssStyle_ != null ? cssStyle_.fontWeight_ : null);
			g.drawText(Utils.abbrevString(descriptionText_, font_, width_ - marginLeft_ - marginRight_),
					((width_ - (int) font_.measureText(descriptionText_)) >> 1), posY + img_.getHeight()+Utils.getScaledValue(10), font_);
			if (notice_ > 0) {
				img_1 = ConstantRepository.decodeBitmap(bv_.getResources(), R.drawable.bg_number);
				g.drawBitmap(img_1, imgWidth - Utils.getSystemScaledValue(20), 4, font_);
				notice_ = notice_ > 99 ? 99 : notice_;
				if(notice_ < 10){
					g.drawText(Integer.toString(notice_), imgWidth - Utils.getSystemScaledValue(12), Utils.getSystemScaledValue(19), font_);
				}else{
					g.drawText(Integer.toString(notice_), imgWidth - Utils.getSystemScaledValue(14), Utils.getSystemScaledValue(19), font_);
				}
			}
		}
	}

	public String getContentText() {
		// TODO Auto-generated method stub
		return null;
	}

	public void setUrl(String url) {
		if (url != null) {
			isHyperLink_ = true;
			linkAddr_ = url;
			focusable_ = true;
		} else {
			isHyperLink_ = false;
			linkAddr_ = null;
			focusable_ = false;
		}
	}

	public void setContentText(String text) {
		// TODO Auto-generated method stub
	}
	
	public void setImage(final Bitmap img) {
		if (null != img) {
			img_ = img;
		}
		height_ = img_.getHeight() + marginTop_ + marginBottom_ + borderWidth_ * 2;
		width_ = img_.getWidth() + marginLeft_ + marginRight_ + borderWidth_ * 2;
		ViewGroup.LayoutParams viewLayoutParams = this.getLayoutParams();
		if (null == viewLayoutParams) {
			viewLayoutParams = new LinearLayout.LayoutParams(width_, height_);
		}
		viewPara_ = viewLayoutParams;

		bv_.runOnUiThread(new Runnable() {
			public void run() {
				setImageBitmap(img_);
				setLayoutParams(viewPara_);
			}
		});
	}
	
	public String getDescriptionText(){
    	return this.descriptionText_;
    }

	public Paint getFont() {
		// TODO Auto-generated method stub
		return font_;
	}

	public void setDescriptionText(String text) {
		if (font_ == null) {
			font_ = new TextPaint();
			font_.setTextSize(Utils.getScaledValue(ConfigManager.SIZE_SMALL));
		}
		descriptionText_ = text;
		// reCalcuHeight();
	}

	void setFocusImage(Bitmap focusImage) {
		focusImg_ = focusImage;
	}

	public void shrinkWidth() {
		// TODO Auto-generated method stub

	}

	public void cleanText() {
		// TODO Auto-generated method stub

	}

	public void mould() {
		CssStyle cs = cssStyle_;

		// set the background color.
		if (cs != null) {
			if ( cs.bgColor_ != 0) {
				setBackgroundColor(cs.bgColor_);
			}
			if (cs.width_ > 0) {
				width_ = cs.width_;
			}
			if (cs.height_ > 0) {
				height_ = cs.height_;
			}
		}

		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(width_, height_);
		setLayoutParams(lp);
	}

	public void setCmpHeidht(int height) {
		// TODO Auto-generated method stub

	}

	public void setCmpWidth(int width) {
		// TODO Auto-generated method stub

	}

	public void setCssStyle(CssStyle style) {
		cssStyle_ = style;
	}

	public View getCmpView() {
		return this;
	}

	public int getCmpHeight() {
		return height_;
	}

	public int getCmpWidth() {
		// TODO Auto-generated method stub
		return width_;
	}
	
	public void setInTable(boolean inTable) {
		isInTable_ = inTable;
	}

	public boolean isInTable() {
		return isInTable_;
	}

	public CssStyle getCssStyle() {
		return cssStyle_;
	}

	public void releaseResource(View view) {
		if (null != focusImg_ && !focusImg_.isRecycled()) {
			focusImg_.recycle();
		}
		focusImg_ = null;
		if (null != img_ && !img_.isRecycled()) {
			img_.recycle();
		}
		img_ = null;
		if (null != img_1 && !img_1.isRecycled()) {
			img_1.recycle();
		}
		img_1 = null;
		if (null != img_2 && !img_2.isRecycled()) {
			img_2.recycle();
		}
		img_2 = null;
		
	}

	public void setForm(FormLayout formLayout) {
		// TODO Auto-generated method stub

	}

	public FormLayout getForm() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void setEncrypt(String encrypt) {
		// TODO Auto-generated method stub
		isEncrypt_ = encrypt;
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		
		int touchEvent = event.getAction();
		switch (touchEvent) {
		case MotionEvent.ACTION_DOWN:
			touchDownX_ = (int) event.getX();
			touchDownY_ = (int) event.getY();
			if (null != link_){
				return true;
			}
		case MotionEvent.ACTION_UP:
			touchUpX_ = (int) event.getX();
			touchUpY_ = (int) event.getY();
			if ((null != link_) && (Math.abs(touchUpY_ - touchDownY_)<10) && (Math.abs(touchUpX_ - touchDownX_)<10)){
				if (bv_ instanceof DetailView){
					DetailView bv = (DetailView)bv_;
					bv.dealWithLink(link_, bv, false);
					return true;
				}
			}else{
				return false;
			}
		}
		return false;
	}

}
